﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using WeatherStation;

namespace WeatherStationView
{
    /// <summary>
    /// Interaction logic for StatisticsDisplay.xaml
    /// </summary>
    public partial class StatisticsDisplay : UserControl, IObserver
    {
        public StatisticsDisplay()
        {
            InitializeComponent();
        }

        #region StatisticsVariables and properties

        private float maxTemp = 0.0f;
        private float minTemp = 200;//set intial high so that minimum 
        //is set first invokation
        private float temperatureSum = 0.0f;
        private int numReadings = 0;

        #endregion

        #region IObserver Members

        public void Update(float temperature, float humidity, float pressure)
        {
            temperatureSum += temperature;
            numReadings++;
            float average = temperatureSum / numReadings;

            if (temperature > maxTemp)
            {
                maxTemp = temperature;
            }

            if (temperature < minTemp)
            {
                minTemp = temperature;
            }

            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new setValuesDelegate(setValues), maxTemp, minTemp, average);
        }

        #endregion

        #region WeatherData suscription related

        private Boolean suscribedToWD = false;

        private ISubject wd;

        public ISubject WeatherDataSource
        {
            get
            {
                return wd;
            }
            set
            {
                wd = value;
            }
        }

        #endregion

        #region GUI logic

        private delegate void setValuesDelegate(float max, float min, float average);

        private void setValues(float max, float min, float average)
        {
            if (average < pgbAverage.Minimum || average > pgbAverage.Maximum ||
                max < pgbAverage.Minimum || max > pgbAverage.Maximum ||
                min < pgbAverage.Minimum || min > pgbAverage.Maximum)
                return;

            ///assign the average...
            pgbAverage.Value = average;
            lblAverage.Content = average.ToString("#0.00") + "º";

            ///get some shit...
            double realWidth = pgbAverage.ActualWidth;
            double pixelsPerUnit = realWidth / (pgbAverage.Maximum - pgbAverage.Minimum);

            ///calculate position for maximum...
            recMaximum.Margin = new Thickness(pixelsPerUnit * (max - pgbAverage.Minimum) - recMaximum.Width / 2,
                0, 0, 0);
            lblMaximum.Content = max.ToString("#0.00") + "º";

            ///calculate position for minimum...
            recMinimum.Margin = new Thickness(pixelsPerUnit * (min - pgbAverage.Minimum) - recMaximum.Width / 2,
                0, 0, 0);
            lblMinimum.Content = min.ToString("#0.00") + "º";
        }

        private void btnSuscribeUnsuscribe_Click(object sender, RoutedEventArgs e)
        {
            if (wd == null) return;

            if (suscribedToWD)
            {
                wd.RemoveObserver(this);
                btnSuscribeUnsuscribe.Content = "Suscribe to Weather Data Source";
            }
            else
            {
                wd.RegisterObserver(this);
                btnSuscribeUnsuscribe.Content = "Unsuscribe from Weather Data Source";
            }

            suscribedToWD = !suscribedToWD;
        }

        #endregion

    }
}
